package cn.com.bluemoon.daps.system.service.impl;

import cn.com.bluemoon.daps.domp.api.LocalDompService;
import cn.com.bluemoon.daps.domp.api.LocalUserService;
import cn.com.bluemoon.daps.system.entity.DapSysUser;
import cn.com.bluemoon.daps.system.service.DapSysUserService;
import cn.com.bluemoon.daps.system.utils.JwtUtils;
import cn.com.bluemoon.metadata.common.ResultBean;
import cn.com.bluemoon.metadata.common.exception.DescribeException;
import com.auth0.jwt.interfaces.Claim;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Lilike on 2023/1/2
 */
@Service
public class LocalUserServiceImpl {

    @Autowired
    private DapSysUserService dapSysUserService;

    @Autowired
    private LocalDompService localDompService;

    public String isLoginUser(String token) {
        Map<String, Claim> verifyToken = JwtUtils.verifyToken(token);
        if (verifyToken == null) {
            return null;
        }
        return verifyToken.get(JwtUtils.ACCOUNT).asString();
    }



    public List<Map<String, Object>> getUserInfoById(String userId) {

        DapSysUser one = dapSysUserService.lambdaQuery().eq(DapSysUser::getAccount, userId).one();
        if (one == null) {
            throw new DescribeException("用户不存在", ResultBean.FAIL_CODE);
        }

        List<Map<String, Object>> res = Lists.newArrayList();
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("ID",one.getAccount());
        resMap.put("MOBILE",one.getMobile());
        resMap.put("USER_NAME",one.getUserName());
        resMap.put("EMAIL",one.getEmail());
        res.add(resMap);
        return res;
    }

    public String login(String userName, String passWord) {
        return localDompService.dompLogin(userName,passWord,null).getToken();
    }
}
